﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 4. Write a script that finds the maximal increasing 
			sequence in an array. Example:  
			{3, 2, 3, 4, 2, 2, 4} -> {2, 3, 4}.  -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		var array = new Array();
		var number = parseInt(prompt("Enter number of elements"));
		if (!isNaN(number)) {
			// Enter the array of numbers
			for (var i = 0, j = 0; i < number; i++) {
				var n = parseInt(prompt("Enter element " + i));
				if (!isNaN(n)) {
					array[j] = n;
					j++;
				}
			}

			jsConsole.writeLine("You have entered: ");
			jsConsole.writeLine(array.join());
			jsConsole.writeLine("");

			// Find maximal increasing sequence
			var i = 0;
			var startIndex = 0;
			var previousElement = array[startIndex];

			var sequentialLength = 1;
			var sequentialLengthMax = 1;

			for (i = 1; i < array.length; i++) {
				if (array[i] == (previousElement + 1))
				{ sequentialLength++; }
				else
				{
					if (sequentialLength > sequentialLengthMax) {
						sequentialLengthMax = sequentialLength;
						startIndex = i - sequentialLength;
					}

					sequentialLength = 1;
				}

				previousElement = array[i];
			}

			if (sequentialLength > sequentialLengthMax) {
				sequentialLengthMax = sequentialLength;
				startIndex = i - sequentialLength;
			}

			jsConsole.writeLine("The maximal increasing sequence is: ");
			for (i = startIndex; i < (sequentialLengthMax + startIndex) ; i++) {
				jsConsole.writeLine(array[i] + " ");
			}
		}
		else {
			jsConsole.writeLine("Wrong Input.");
		}
	</script>
</body>
</html>
